package com.Ugent.Minerva;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Links extends ListActivity {
	TextView myLinks;
	TextView gebruiker;
	private LinkAdapter adapter;
	private ArrayList<Link> links = new ArrayList<Link>();

	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.linksmain);
	  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

	  Student s1 = (Student) getIntent().getSerializableExtra("student");
	  //get links for a course
	  links = (ArrayList<Link>) getIntent().getSerializableExtra("links");
	  //adapter for listview
	  this.adapter = new LinkAdapter (this, R.layout.linkslist, links);
	  setListAdapter(this.adapter);
	  ListView lv = getListView();

	  myLinks = (TextView) findViewById(R.id.links); 
	  gebruiker = (TextView) findViewById(R.id.gebruiker);
	  gebruiker.setText(s1.getName());
	  
	  //make link active to native browser
	  lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {	  
        	  Link l = links.get(position);
        	  String url = l.getLink();
        	  
        	  Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse(url));
        	  startActivity(browserIntent);

          }
      });

	}
	
	public void clickHandler(View v){    	
	    
		startActivity(new Intent(getApplicationContext(), Account.class));
	
	} 
	
	//adapter for links
	private class LinkAdapter extends ArrayAdapter<Link> {

    private ArrayList<Link> items;

    public LinkAdapter(Context context, int textViewResourceId, ArrayList<Link> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.linkslist, null);
            }
            Link l = items.get(position);
            if (l != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                    if (tt != null) {
                          tt.setText(l.getTitle());                            }
                    if(bt != null){
                          bt.setText(l.getDescription());
                    }
            }
            return v;
    }
}
}

